<template>
  <div class="vip_dl">
    <div class="container">
      <div class="row">
        <div class="vip_dl_con">
          <div class="vip_dl_fr">
            <div class="vip_dl_fr2">
              <h1 class="hidden-xs">向我们介绍你自己</h1>
              <ul class="welcome">
                提供有关您自己的信息是注册的第一步
              </ul>
              <div class="denglu block">
                <div class="denglu1">
                  <el-form :rules="rules" :model="loginData" ref="ruleFormRef"  >
                    <div class="denglu1_con1 block">
                        <el-form-item prop="email" label="电子邮箱">
                            <el-input
                              v-model="loginData.email"
                              placeholder="请输入电子邮箱"
                            >
                            <template #suffix>
                              <el-button
                                type="primary"
                                class="fasong"
                                :disabled="timerOn"
                                @click="sendCode"
                                > {{ timerOn ? `${countdown}秒后重新获取` : '获取验证码' }}</el-button>
                              </template>
                          </el-input>
                          </el-form-item>
                          <el-form-item prop="code" label="验证号码">
                            <el-input
                              v-model="loginData.code"
                              placeholder="请输入验证号码"
                            ></el-input>
                          </el-form-item>
                          <el-form-item prop="password" label="账号密码">
                            <el-input
                              type="password"
                              v-model="loginData.password"
                              placeholder="请输入登录密码"
                              style="color: #a9a9a9"
                            ></el-input>
                          </el-form-item>
                          <el-form-item prop="confirmPsd" label="密码确认">
                            <el-input
                              type="password"
                              v-model="loginData.confirmPsd"
                              placeholder="请输入验证码"
                            ></el-input>
                          </el-form-item>

                          <div class="agreement">
                            <span>如果您已使用该邮箱注册过账号，请返回上一页面进行 
                               <router-link to="/login">
                                 <span class="login">登录</span> 
                               </router-link>
                               
                              </span>

                          </div>

                         <div class="li5">
                          <el-button
                            type="primary"
                            @click="regUser(ruleFormRef)"
                            class="fasong"
                            >立即注册</el-button>
                         </div>


                    </div>
                  </el-form>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ElMessage, FormRules, FormInstance } from "element-plus";
 import { regApi,sendCaptchaApi} from "@/api/login/login";
import type { RegParam} from "@/api/login/login";
 const router = useRouter();
const ruleFormRef = ref<FormInstance>();
const rules = reactive<FormRules<RegParam>>({
  email: [
    {
      required: true,
      message: "请输入电子邮箱",
      trigger: "blur",
    },
    {type: "email", message: "请输入正确的邮箱格式", trigger: "blur"}
  ],
  password: [{ required: true, message: "请输入登录密码", trigger: "blur" }],
  confirmPsd: [{ required: true, message: "请输入确认密码", trigger: "blur" }],
  code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
});

const timerOn = ref<boolean>(false);
const countdown = ref<number>(60);
  let intervalId=1

async   function sendCode() {
  if (loginData.value.email === "") {
    ElMessage.error('请输入电子邮箱')
    return;
  }
  if (!timerOn.value) {
    timerOn.value = true;
    countdown.value = 60;
     intervalId = setInterval(() => {
      if (countdown.value > 0) {
        countdown.value--;
      } else {
        clearInterval(intervalId);
        timerOn.value = false;
      }
    }, 1000);
}
 const res= await sendCaptchaApi(loginData.value.email)
 if(res.code===200){
   ElMessage.success(res.msg)
 }else{
   ElMessage.error(res.msg)
 }
}
 
const loginData = ref<RegParam>({email: "",password: "",confirmPsd: "",code:''});

async function regUser(formEl: FormInstance | undefined) {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
       const response= await regApi(loginData.value)
       console.log(response)
      if(response.code===200){
         ElMessage.success('注册成功');
       router.push("/login");
      }else{
        ElMessage.error(response.msg)
      }
    } else {
      console.log("error submit!", fields);
    }
  });
}




</script>
<style scoped lang="less">
.vip_dl {
  width: 100%;
  background: url("../assets/img/1366-675.png") no-repeat center top;
  background-size: 100% 100%;
  height: 675px;
  display: flex;
  margin-top: -10px;
}
.vip_dl .container {
  width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  align-content: center;
}
.vip_dl .container .row {
  display: flex;
  flex-direction: column;
  margin: 0;
}
.vip_dl_con {
  width: 800px;
}
.hidden-xs {
  color: #00499d;
  font-size: 26px;
  text-align: center;
  margin-bottom: 20px;
  font-weight: 800;
}
.el-input {
  height: 40px;
}
.vip_dl_fr {
  width: 60%;
  margin: 0 auto;
  background: #fff;
  padding: 25px 0;
  border-radius: 10px;
}
.vip_dl_fr2 {
  width: 80%;
  margin: 0 auto;
  padding: 10px 0 15px 0;
}
.vip_dl_fr2 > form > div.block {
  display: block;
}
.denglu1 > form > div {
  display: none;
}
.denglu1 > form > div.block {
  display: block;
}
.zhuce_ul li {
  width: 100%;
  margin-top: 15px;
}
.zhuce_ul li input {
  width: 100%;
  font-size: 14px;
  padding-left: 5%;
  border: 1px solid #e4e4e4;
  border-radius: 2px;
  height: 48px;
  line-height: 48px;
  background: #eff0f4 !important;
}
.li3 {
  position: relative;
}
.zhuce_ul li {
  width: 100%;
  margin-top: 15px;
}
.li3 .yzm {
  position: absolute;
  top: 0;
  right: 0;
  width: 40%;
  text-align: center;
  line-height: 45px;
  color: #6f727e;
  text-decoration: underline;
  font-size: 14px;
  background-color: Transparent;
  text-transform: none;
  outline: none;
  border: none;
}

.li5 .fasong {
  height: 50px;

  text-align: center;
  color: #fff;
  border-radius: 2px;
  background: #00499d;
  display: inline-block;
  width: 100%;
  font-size: 20px;
  border: 0;
  outline: 0;
}

.denglu1_con1 .li4 {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-direction: row;
  width: 100%;
  margin-top: 10px;
}

.agreement{
  margin-bottom: 20px;
  font-size: 14px;
  color: #999;
}
.login{
  font-size: 18px;
    color: #345aa3;
    cursor: pointer;
}

.denglu1_con1 .li4 .agreement {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.denglu1_con1 .li4 .agreement label {
  margin: 0;
  display: flex;
  align-items: center;
  padding: 0;
}
.denglu1_con1 .li4 .agreement input {
  width: 13px;
  height: 13px;
  vertical-align: -2px;
  margin-right: 5px;
  float: left;
  padding-left: 0;
}
.denglu1_con1 .li4 span {
  font-size: 14px;
  display: inline-block;
  cursor: pointer;
  text-align: center;
  color: #6f727e;
}
.denglu1_con1 .li4 span i:hover {
  color: #007aff;
}
.agreement span i {
  font-style: normal;
  color: #007aff;
}
.zhuce_ul li.li3 {
  display: flex;
  line-height: 48px;
}
.zhuce_ul li.li3 input {
  width: 60%;
}
.zhuce_ul li.li3 img {
  height: 40px;
  margin-left: 15px;
}
.vip_dl_fr2 .welcome {
  font-family: "Microsoft Yahei", Tahoma, Helvetica, Arial, sans-serif;
  font-size: 14px;
  background: #fff;
  color: #333;
  border-radius: 2px;
  padding: 0 0 5px 0;
  text-align: left;
}
</style>
